<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('业务流程配置') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('业务类型')" prop="busType">
        <el-select v-model="queryParams.busType" :placeholder="$t('请选择')" clearable size="small">
          <el-option
            v-for="dict in wfBusFlowTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:busflow:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="wfConfigFlowList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column :label="$t('归属部门')" align="center" prop="deptName" />
      <el-table-column :label="$t('业务类型')" align="center" prop="busType" :formatter="busTypeFormat" />
      <el-table-column :label="$t('业务数据类型')" align="center" prop="busDataType" :formatter="busDataTypeFormat" />
      <el-table-column :label="$t('版本号')" align="center" prop="version" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['act:busflow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['act:busflow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateNodeHandle(scope.row)"
          >{{ $t('节点配置') }}</el-button>
          <el-button
            v-hasPermi="['act:busflow:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务流程配置对话框 -->
    <el-dialog
      :title="$t('业务流程配置')"
      :visible.sync="formOpen"
      width="500px"
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <wfConfigFlowForm ref="wfConfigFlowForm" />
    </el-dialog>

    <!-- 节点配置 -->
    <el-dialog
      :visible.sync="nodeFormOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="wfConfigNodeDialog"
      :append-to-body="true"
    >
      <wfConfigNodeList ref="wfConfigNodeList" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listWfConfigFlow,
  delWfConfigFlow
} from '@/api/act/busFlow/wfConfigFlow'
import wfConfigFlowForm from './wfConfigFlowForm.vue'
import wfConfigNodeList from './wfConfigNodeIndex'

export default {
  components: {
    wfConfigFlowForm,
    wfConfigNodeList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 业务流程配置表格数据
      wfConfigFlowList: [],
      // 编辑节点页面是否展示
      nodeFormOpen: false,
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        busType: undefined,
        version: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 业务类型字典
      wfBusFlowTypeOptions: [],
      // 业务数据类型字典
      wfBusDataTypeOptions: {}
    }
  },
  async created() {
    const response = await this.getDicts('wf_bus_flow_type')
    this.wfBusFlowTypeOptions = response.data
    await Promise.all(this.getWfBusDataTypeOptions())
    this.getList()
  },
  methods: {
    /** 查询业务流程配置列表 */
    getList() {
      this.loading = true
      listWfConfigFlow(this.queryParams).then(response => {
        this.wfConfigFlowList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        busType: undefined,
        version: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.wfConfigFlowForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.wfConfigFlowForm.init(row.id)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delWfConfigFlow(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    // 节点配置
    updateNodeHandle(row) {
      this.nodeFormOpen = true
      localStorage.setItem('wfConfigFlowBusType', row.busType)
      this.$nextTick(() => {
        this.$refs.wfConfigNodeList.init(row.id)
      })
    },
    // 格式化业务类型
    busTypeFormat(row) {
      return this.selectDictLabel(this.wfBusFlowTypeOptions, row.busType)
    },
    // 查询业务类型
    getWfBusDataTypeOptions() {
      return this.wfBusFlowTypeOptions.reduce((results, item) => {
        return [
          ...results,
          (() => new Promise(resolve => {
              const childDict = item.childDict
              const childValue = item.dictValue
              this.getDicts(childDict).then(response => {
                this.wfBusDataTypeOptions[childValue] = response.data
                resolve()
              })
            }))()
        ]
      }, [])
    },
    // 格式化业务数据类型
    busDataTypeFormat(row) {
      return this.selectDictLabel(this.wfBusDataTypeOptions[row.busType], row.busDataType)
    }
  }
}
</script>
